//index.js
const request = require('../../utils/request.js')
const config = require('../../config.js');
import { bindPhone, getValidateCode } from '../../static/api/api.js'
let t;
//获取应用实例
var app = getApp()
Page({
    data: {
        phoneVal: '',
        verificationCode: '',
        countSeconds: 60,
        verifiyBtnDisable: 0,
        isChecked: true,
        params:null
    },
    onLoad: function(options) {
        let that = this;
        this.setData({
            params:options
        })
        // app.getUserInfo(function(userInf) {
        //     console.log(userInf)
        //     that.setData({
        //         userInf: userInf
        //     })
        // });
    },
    //跳转协议内容页面
    goProtocol: function() {
        wx.navigateTo({
            url: '../user_protocol/user_protocol'
        })
    },
    //点击选中同意单选框
    checkedProtocol: function() {
        let isSelected = this.data.isChecked;
        this.setData({
            isChecked: !isSelected
        })
    },
    //获取验证码
    getValidateCode: function(unicode) {
        if (this.data.phoneVal == '') {
            wx.showToast({
                icon:"none",
                title: '请输入手机号码',
            })
            return
        }
        if (!(/^1[345789]\d{9}$/.test(this.data.phoneVal))) {
            wx.showToast({
                icon: "none",
                title: '手机号码有误'
            })
            return
        }

        if (this.data.verifiyBtnDisable == '1') return;

        this.setData({
            verifiyBtnDisable: 1
        })
        let countTime = 60;
        let that = this;
        that.setData({
            countDownText: '(' + countTime + ')'
        })
        wx.showLoading({
            title: '加载中...',
        })
        let params={
            phone: this.data.phoneVal,
            type:4
            
        }
        getValidateCode(params).then(response => {
            wx.hideLoading();
            wx.showToast({
                icon: "none",
                title: '验证码发送成功，请注意查收短信验证码',
            })

        }).catch(response => {
            wx.hideLoading();
        })
        
        t = setInterval(function() {
            countTime--;
            that.setData({
                countDownText: '(' + countTime + ')'
            })
            if (countTime < 0) {
                that.setData({
                    verifiyBtnDisable: 0
                })
                clearInterval(t);
            }
        }, 1000)
    },
    //开始绑定按钮事件
    startBindPhone: function() {
        let that = this
        if (this.data.phoneVal == '') {
            wx.showToast({
                icon:"none",
                title: '手机号不能为空'
            })
            return
        }
        if (!(/^1[345789]\d{9}$/.test(this.data.phoneVal))) {
            wx.showToast({
                icon: "none",
                title: '手机号码有误'
            })
            return
        }
        if (this.data.verificationCode == '') {
            wx.showToast({
                icon: "none",
                title: '验证码不能为空',
            })
            return
        }
        wx.showLoading({
            title: '加载中...',
        })
        let params = {
            phone: this.data.phoneVal,
            type: 1,
            code: this.data.verificationCode
        }
        bindPhone(params).then(response => {
            wx.hideLoading();
            console.log(response)
            wx.showToast({
                icon: 'none',
                title: '绑定成功',
            })
            setTimeout(function(){
                wx.navigateTo({
                    url: '/pages/author_page/author_page',
                })
            },1000)
            
            
        }).catch(response => {
            wx.hideLoading();
        })
    },
    //手机号码
    bindKeyInput: function(e) {
        this.setData({
            phoneVal: e.detail.value
        })
    },
    //验证码
    bindKeyValidate: function(e) {
        this.setData({
            verificationCode: e.detail.value
        })
    },
    onUnload:function(){
        wx.redirectTo({
            url: '/pages/shop_index/shop_index',
          })
    },
   
})